#!/bin/env  python
# -*- coding: gbk -*- 
############################################################                                         
#   
# @file:    atscript
# @author:  zhenghaibing(zhenghaibing02@baidu.com)
# @date:    2011/06/16 
# @version: 1.0.0
# @bried:  dlb-mrframework½Å±¾×Ô¶¯»¯
#
############################################################

import os
import os.path
import subprocess
import filecmp
import sys

def expect_file_equal(file1, file2):
    return filecmp.cmp(file1, file2)
        
def expect_file_exsits(filelist):
    for file in filelist:
        if not os.path.exists(file):
            return False

class base:
    def __init__(self, start_mr_conf, schedule_file, joblist_file):
        self.start_mr_conf = start_mr_conf
        self.schedule_file = schedule_file
        self.joblist_file = joblist_file
        self.schedule = {}
        self.joblist = {}

	try:
	    self.parse_conf()
        except IOError, e:
            print "I/O error: ", e.message

    def parse_conf(self):
        "parse config file need by mrframework, get the schedule file, the joblist file"
        self.check_conf()
        self.parse_schedule()
        self.parse_joblist()

    def check_conf(self):
        if not (os.path.exists(self.start_mr_conf) and \
           os.path.exists(self.schedule_file) and os.path.exists(self.joblist_file)):
             raise IOError("the conf file need by mrframework is not exists")
    def parse_schedule(self):
	"""
        #   [Schedule:Master] prepare_cir
	#   [Schedule:Serial] cir
        """
        type = ['Master', 'Serial']
        file = open(self.schedule_file)
        try:
            while True:
                line = file.readline()
                if not line:
                    break
                if not line.strip().startswith('[Schedule:'):
                    continue
                pure_line = line.strip().split()
		"key = jobname, value = 'Master' or 'Serial', eg. key=prepare_cir, value=Master"
                key = pure_line[1]
                value = pure_line[0].split(':')[1][:-1]
                if value not in type:
                    continue
                self.schedule[key] = value
        except IOError, e:
            raise IOError("parse schedule file %s error: %s") % (self.schedule_file, e.strerror)
        finally:
            file.close()       

    def parse_joblist(self):
        pass

class at_case():
    """
       input_type = 'HDFS' or 'LOCAL'
       if input was a HDFS path, then input_tpye should be equal 'HDFS'
       
       ²½Öè£º1. ×¼±¸ÊäÈë
             2. ÔËÐÐ
             3. check result
   """
    def __init__(self, casename, base, jobname, input, output):
        self.base = base
        self.jobname = jobname
	if self.jobname not in self.base.schedule.keys():
            raise IOError("can not find job %s in %s" %(self.jobname, self.base.schedule_file))

        self.casename = casename
        self.input = input
        self.output = output
        self.run_cmd = "sh -x /home/spider/dlb-mrframework/bin/start.sh %s -p %s -o 1>out 2>err"  % (self.base.start_mr_conf, self.jobname)
    def set_input(self):
        pass

    def run(self):
        pass
	
    def get_output(self):
        pass

    def check_result(self):
        pass
 
    def execute_cmd(self, cmd, flag=False):
        "if flag==True && p.wait()!= 0  , then raise error"
        p = subprocess.Popen(cmd, shell=True)
        if p.wait() !=0 :
            print >>sys.stderr, "%s failed" % cmd
            if flag:
                raise Error("%s failed" % cmd)

if __name__ == "__main__":
    sample = base("/home/spider/rankget/conf/rankget_hadoop.conf", "/home/spider/rankget/conf/rankget_schedule.conf","/home/spider/rankget/conf/rankget_joblist.conf")
    #print len(sample.schedule.keys())
    #print sample.schedule.keys()
    at = at_case('TEST_rg_getsite', sample, 'rg_getsite', '/user/spider/rankget/input/', 'LIST', '/user/spider/rankget/output/site_to_train/', 'HDFS')
    at.set_input()
    at.run()
    at.get_output()
    at.check_result()
